import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_icon_title_view.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/ins_task_detail_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/responsibility/task_list/task_list_controller.dart';

/// 包保责任任务列表
class TaskListPage extends StatelessWidget {
  TaskListPage({required this.type, required this.globalKey, super.key});

  // 1:主要负责人 2:技术负责人 3:操作负责人
  final int type;
  final GlobalKey<CustomEasyRefreshListState> globalKey;
  final controller = Get.put(TaskListController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        initState: (state) {
          controller.initData(globalKey);
        },
        builder: (_) {
          return Container(
            padding: EdgeInsets.symmetric(horizontal: 14.w),
            child: CustomEasyRefreshList<InsTaskDetailEntity>(
              key: globalKey,
              dataLoad: (pageIndex, pageSize, {String? search}) async {
                return controller.getListData(pageIndex, pageSize, search: search, type: type);
              },
              isShowSearch: true,
              itemBuilder: (InsTaskDetailEntity item, int index) {
                return CustomTitleChartPage(
                  titleView: CustomIconTitleView(
                    title: item.troubleshootContent ?? '',
                    titleRightWidget: _getStateView(item.checkStatus ?? 0),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Text(
                            '到期时间：',
                            style: TextStyle(
                              fontSize: 13.sp,
                              height: 1.2,
                              fontWeight: FontWeight.w400,
                              color: const Color(0xffA5A6B5),
                            ),
                          ),
                          Expanded(
                              child: Text(
                            item.workEndTime ?? '',
                            textAlign: TextAlign.right,
                            style: TextStyle(
                              fontSize: 12.sp,
                              height: 1.2,
                              fontWeight: FontWeight.w400,
                              color: const Color(0xff495061),
                            ),
                          ))
                        ],
                      ),
                    ],
                  ),
                ).clickOnce(() {
                  controller.toDetailsPage(item.id ?? '');
                });
              },
            ),
          );
        });
  }

  /// 正常：0；存在隐患：1；未排查(漏检)：2；逾期排查(补检)：3(巡检计划台账中)；待排查：4
  Widget _getStateView(int type) {
    String stateText = '';
    Color? color;
    switch (type) {
      case 0:
        stateText = '正常';
        color = const Color(0xff00CE78);
        break;
      case 1:
        stateText = '存在隐患';
        color = const Color(0xffFD6B6B);
        break;
      case 2:
        stateText = '漏检';
        color = const Color(0xff5C6DFF);
        break;
      case 3:
        stateText = '补查';
        color = const Color(0xffFC9900);
        break;
      case 4:
        stateText = '待排查';
        color = const Color(0xffA5A9C8);
        break;
      default:
        break;
    }
    if (stateText.isEmpty) {
      return Container();
    }
    return Container(
      height: 18.w,
      width: 52.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(8.w),
          topRight: Radius.circular(8.w),
          bottomRight: Radius.circular(8.w),
        ),
      ),
      child: Text(
        stateText,
        style: TextStyle(
          color: const Color(0xffFFFFFF),
          height: 1.2,
          fontWeight: FontWeight.w400,
          fontSize: 10.sp,
        ),
      ),
    );
  }
}
